package com.taldh;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Iterator;
import java.util.Set;

public class BatchDelRedis {
    public static void main(String[] args) {
        batchDel("mall:user:userId:WDPH:");
    }

    public static long batchDel(String key){

        // 池基本配置
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(5);
        config.setMaxWaitMillis(1000L);
        config.setTestOnBorrow(false);

        long result = 0;
        JedisPool jedisPool = new JedisPool(config, "10.53.156.154", 7003, 10000,"123qwe");

        try (Jedis jedis = jedisPool.getResource()) {
            Set<String> set = jedis.keys(key + "*");
            Iterator<String> it = set.iterator();
            while(it.hasNext()){
                String keyStr = it.next();
                System.out.println(keyStr);
                result += jedis.del(keyStr);
            }
        } catch (Exception e){
        } finally {
        }
        return result;
    }
}
